from django.shortcuts import render
from django.http import HttpResponse
from .models import keylogin
from .models import Main_News
# Create your views here.

def hello(requset):
    return HttpResponse("Hello World")

def index(request):
    """
    登陆界面跳转函数
    :param request:
    :return:
    """
    connect = {
        "message":"请输入账户名和密码"
    }
    #关闭浏览器则清除
    return render(request, 'blog/index/index.html', connect)

def main(request):
    """
    主界面跳转函数
    :param request:
    :return:
    """
    cookie = request.session.session_key
    if request.method =="POST":
        getname=request.POST['username']
        getpassword=request.POST['password']

        try:
            catchname = keylogin.objects.get(user_number=getname)
        except:
            return render(request, 'blog/index/index.html', {"message": "请输入正确的账户名和密码"})

    else:
        print(request.method)
    #获取公告信息
    PublicList = Main_News.objects.filter(id__lte=5)
    request.session.set_expiry(0)
    request.session.clear_expired()
    request.session["name"] = getname

    connct = {
        "newlist":PublicList,
        "namename":getname
    }
    for Dict in PublicList:
        print(Dict)
    return render(request, 'blog/main/main.html', connct)

def news(request,number):
    """
    新闻界面
    :param request:
    :param number:
    :return:
    """
    getmessage = Main_News.objects.get(id=number)
    print(getmessage)
    connect = {
        "getmessage":getmessage
    }
    return render(request,'blog/main/news.html',connect)
